home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #3 / Amiga Plus CD - 2002 - No. 03.iso / AmiSoft / Dev / E / Goportscan_src.lha / GoPortscan_Src / EXPERIMENTAL / synscan.e < prev   
Encoding:
Text File  |  2003-02-02  |  7.1 KB  |  210 lines

  1.  
  2. /*
  3.     Description: Some experimental procedures for doing SYN scans.
  4.                  These procedures were part of the main GP source, but
  5.                  were removed as they were never completed.
  6.  
  7.                  NOTE: The only way to listen for replies in a SYN
  8.                  scan is by sniffing. It cannot be done directly through
  9.                  the socket API
  10. */
  11.  
  12.  
  13.  
  14.  
  15. PROC synscan(portptr:PTR TO portentry) HANDLE
  16. DEF errbuf[PCAP_ERRBUF_SIZE]:STRING,
  17.     dev[5]:STRING,
  18.     ret,
  19.     netp:PTR TO LONG,
  20.     maskp:PTR TO LONG,
  21.     descr,
  22.     hdr:PTR TO pcap_pkthdr
  23.  
  24. IF (miamibase:=OpenLibrary('miami.library', NIL)) = NIL THEN Raise(ERR_NOMIAMI)
  25. IF (miamipcapbase:=OpenLibrary('miamipcap.library', NIL)) = NIL THEN Raise(ERR_NOPCAP)
  26.  
  27.         hdr:=New(2500)
  28.  
  29.         MiamiPCapInit(miamibase, socketbase)
  30.         StrCopy(dev,MiamiPCapLookupdev(errbuf))
  31.  
  32.         IF dev=NIL
  33.             outlist(errbuf)
  34.         ELSE
  35.             outlist(dev)
  36.             ret:=MiamiPCapLookupnet(dev, {netp}, {maskp}, errbuf)
  37.             IF ret = -1
  38.                 outlist(errbuf)
  39.             ELSE
  40.                 ->addr.addr := netp
  41.                 outlist(Inet_NtoA(netp))
  42.                 outlist(Inet_NtoA(maskp))
  43.                 descr:=MiamiPCapOpenLive(dev, 1500, NIL, -1, errbuf)
  44.                 IF (descr = NIL)
  45.                     outlist(errbuf)
  46.                 ELSE
  47.                     WriteF('Max Snaplen = \d\n', MiamiPCapSnapshot(descr))
  48.                     /*
  49.                     IF (MiamiPCapNext(descr, hdr) = NIL)
  50.                         outlist('No packet capture')
  51.                     ELSE
  52.                         outlist('YAY')
  53.                     ENDIF
  54.                     */
  55.                 ENDIF
  56.             ENDIF
  57.         ENDIF
  58. EXCEPT DO
  59.     IF (miamipcapbase) THEN CloseLibrary(miamipcapbase)
  60.     IF (miamibase) THEN CloseLibrary(miamibase)
  61. SELECT exception
  62.     CASE ERR_NOERROR
  63.         -> Normal exception on exit
  64.     CASE ERR_NOMIAMI
  65.         outlist('THIS FUNCTION REQUIRES A HIGHLY ADVANCED TCP/IP STACK')
  66.         outlist('For example, Miami or MiamiDX')
  67.         outlist('miami.library V6+ cannot be found and/or opened')
  68.     CASE ERR_NOPCAP
  69.         outlist('THIS FUNCTION REQUIRES A HIGHLY ADVANCED TCP/IP STACK')
  70.         outlist('Only Miami and Miami DX \ebREGISTERED\en are advanced enough')
  71.         outlist('miamipcap.library cannot be found and/or opened')
  72.     DEFAULT
  73.         WriteF('Internal Exception in synscan() Code:(\d). Please report problem to author\n',exception)
  74. ENDSELECT
  75.  
  76. ENDPROC
  77.  
  78. ->Version 2
  79.  
  80. PROC synscan(portptr:PTR TO portentry) HANDLE
  81. DEF port=0,
  82.     tcphead:PTR TO tcphdr,
  83.     iphdr:PTR TO ip,
  84.     riphdr:PTR TO ip,
  85.     rtcphead:PTR TO tcphdr,
  86.     pseudohdr:PTR TO pseudo,
  87.     sain:PTR TO sockaddr_in,
  88.     hst:PTR TO hostent,
  89.     sock,
  90.     hostname[64]:STRING,
  91.     on:PTR TO CHAR
  92.  
  93.  
  94. StrCopy(hostname, site)
  95.  
  96. IF (miamibase:=OpenLibrary('miami.library', NIL)) = NIL THEN Raise(ERR_NOMIAMI)
  97. IF (miamipcapbase:=OpenLibrary('miamipcap.library', NIL)) = NIL THEN Raise(ERR_NOPCAP)
  98.  
  99.         MiamiPCapInit(miamibase, socketbase)
  100.         WriteF('\s\n',MiamiPCapLookupdev(hostname))
  101.  
  102.         IF hst:=Gethostbyname(hostname)
  103.             IF (sock:=Socket(AF_INET, SOCK_RAW, IPPROTO_RAW))<>-1
  104.                 on:=1
  105.                 Setsockopt(sock, IPPROTO_IP, IP_HDRINCL, {on}, 4 )
  106.                 sain:=New(SIZEOF sockaddr)
  107.                 sain.family:=AF_INET
  108.                 CopyMem(Long(hst.addr_list), sain.addr, hst.length)
  109.                 ->Bind( sock, sain, SIZEOF sockaddr_in)
  110.  
  111.                 WHILE (portptr <> NIL)
  112.                     FOR port:=portptr.lower TO portptr.upper
  113.  
  114.                         iphdr:=New((SIZEOF ip) + (SIZEOF tcphdr))
  115.                         tcphead:=iphdr + (SIZEOF ip)
  116.                         pseudohdr:=New(SIZEOF pseudo)
  117.  
  118.                         set_ip_hl(iphdr, 5)
  119.                         set_ip_v(iphdr, IPVERSION)
  120.                         iphdr.ttl:=128
  121.                         iphdr.len:=(SIZEOF ip) ->+ (SIZEOF tcphdr)
  122.                         iphdr.id:=90
  123.                         iphdr.p:=IPPROTO_TCP
  124.                         iphdr.src.addr:=Inet_addr('191.168.10.1')
  125.                         iphdr.dst.addr:=Inet_addr('192.168.10.2')
  126.                         iphdr.sum:=cksum(iphdr, SIZEOF ip)
  127.  
  128.                         pseudohdr.src.addr:=Inet_addr('191.168.10.1')
  129.                         pseudohdr.dst.addr:=Inet_addr('192.168.10.2')
  130.                         pseudohdr.place:=0
  131.                         pseudohdr.protocol:=IPPROTO_TCP
  132.                         pseudohdr.len:=(SIZEOF ip)
  133.  
  134.                         tcphead.sport:=1024
  135.                         tcphead.dport:=port
  136.                         tcphead.seq:=TCPSEQNUM
  137.                         tcphead.ack:=0
  138.                         set_tcphdr_off(tcphead, TH_OFFSET)
  139.                         set_tcphdr_x2(tcphead, 0)
  140.                         tcphead.win:=TCP_WINDOW_SIZE
  141.                         tcphead.flags:=%00000010
  142.                         tcphead.urp:=0
  143.                         tcphead.sum:=tcpcksum(tcphead, (SIZEOF tcphdr), pseudohdr, (SIZEOF pseudo))
  144.  
  145.                         Sendto(sock, iphdr, (SIZEOF ip) + (SIZEOF tcphdr) , NIL, sain, SIZEOF sockaddr_in)
  146.  
  147.                         riphdr:=New((SIZEOF ip) + (SIZEOF tcphdr) + 1500)
  148.                         ->Recv(sock, riphdr, (SIZEOF ip) + (SIZEOF tcphdr) + 1500 , NIL)
  149.                         ->rtcphead:=riphdr + riphdr.len
  150.  
  151.                     ENDFOR
  152.                     portptr:=portptr.next
  153.                 ENDWHILE
  154.             ELSE
  155.                 WriteF('Problem with creating the socket')
  156.             ENDIF
  157.         ELSE
  158.             WriteF('Problem with host lookup\n')
  159.         ENDIF
  160.  
  161.         CloseSocket(sock)
  162.  
  163. EXCEPT DO
  164.     IF miamipcapbase THEN CloseLibrary(miamipcapbase)
  165.     IF miamibase THEN CloseLibrary(miamibase)
  166. SELECT exception
  167.     CASE ERR_NOERROR
  168.         -> Normal exception on exit
  169.     CASE ERR_NOMIAMI
  170.         outlist('THIS FUNCTION REQUIRES A HIGHLY ADVANCED TCP/IP STACK')
  171.         outlist('For example, Miami or MiamiDX')
  172.         outlist('miami.library V6+ cannot be found and/or opened')
  173.     CASE ERR_NOPCAP
  174.         outlist('THIS FUNCTION REQUIRES A HIGHLY ADVANCED TCP/IP STACK')
  175.         outlist('Only Miami and Miami DX \ebREGISTERED\en are advanced enough')
  176.         outlist('miamipcap.library cannot be found and/or opened')
  177.     DEFAULT
  178.         WriteF('Internal Exception in synscan() Code:(\d). Please report problem to author\n',exception)
  179. ENDSELECT
  180.  
  181. ENDPROC
  182.  
  183. ->
  184.  
  185. #ifdef TCPCKSUM
  186. PROC tcpcksum(hdr1:PTR TO intfold, hdrsize1:LONG, hdr2:PTR TO intfold, hdrsize2:LONG)
  187. DEF accumulator=0:LONG,
  188.     loop
  189.  
  190.     FOR loop:=0 TO ((hdrsize1-1)/2)
  191.         accumulator:=accumulator+hdr1.arr[loop]
  192.     ENDFOR
  193.  
  194.     FOR loop:=0 TO ((hdrsize2-1)/2)
  195.         accumulator:=accumulator+hdr2.arr[loop]
  196.     ENDFOR
  197.  
  198.     accumulator:=(Shr(accumulator, 16)) + (Eor(accumulator, $FFFF))
  199.     accumulator:=accumulator + (Shr(accumulator, 16))
  200.  
  201.     ->Im not sure why, but with certain sequence numbers the checksum
  202.     ->is always out by a fixed amount
  203.     ->accumulator:=accumulator-2
  204.  
  205.     ->WriteF('len 1 \d           len 2 \d\n', hdrsize1, hdrsize2)
  206.     ->WriteF('TCP Checksum = $\h (\d)\n',accumulator, accumulator)
  207.  
  208. ENDPROC accumulator
  209. #endif
  210.